﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DBAccessLibrary.Model;
using DBAccessLibrary;
using DevExpress.Xpf.Core;
using System.Collections.ObjectModel;
using SQLQuery.Common;

namespace SQLQuery.UI.Comparsion
{
    /// <summary>
    /// DataComparsionConnection.xaml 的交互逻辑
    /// </summary>
    public partial class DataComparsionConnection : DXWindow
    {
        List<ConnectionModel> _connectList = null;
        public DataComparsionConnection()
        {
            InitializeComponent();
            this.DataContext = this;

            DataSource = new ConnectionModel();
            TargetSource = new ConnectionModel();

            _connectList = new List<ConnectionModel>();
            try
            {
                foreach (DataBaseEntity entity in SysCache.ServerList)
                {
                    foreach (DBEntity db in entity.ChildNodes)
                    {
                        _connectList.Add(db.ConnectionSource);
                        this.cbSourceDB.Items.Add(db.ConnectionSource.DataBase);
                        this.cbTargetDB.Items.Add(db.ConnectionSource.DataBase);
                    }
                }
            }
            catch
            {
                MessageBoxHelper.Show("有数据库正在连接，无法显示已连接的信息");
            }
            XmlHelper.Instance.SetTheme(this);
        }

        public ConnectionModel DataSource { get; set; }
        public ConnectionModel TargetSource { get; set; }

        #region event
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(DataSource.DataBase) ||
              string.IsNullOrEmpty(DataSource.UserID) ||
              DataSource.ConnectType == null ||
              string.IsNullOrEmpty(DataSource.Server))
            {
                MessageBoxHelper.Show("源数据参数不完全，请查看");
                return;
            }
            if (string.IsNullOrEmpty(TargetSource.DataBase) ||
             string.IsNullOrEmpty(TargetSource.UserID) ||
             TargetSource.ConnectType == null ||
             string.IsNullOrEmpty(TargetSource.Server))
            {
                MessageBoxHelper.Show("目标数据参数不完全，请查看");
                return;
            }
            if (!DBOperatorFactory.GetOperator.IsConnectDB(DataSource))
            {
                SQLQuery.Common.MessageBoxHelper.Show("源数据 参数错误，无法连接。");
                return;
            }
            if (!DBOperatorFactory.GetOperator.IsConnectDB(TargetSource))
            {
                SQLQuery.Common.MessageBoxHelper.Show("目标数据 参数错误，无法连接。");
                return;
            }
            this.DialogResult = true;
        }

        private void btnCanel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TargetSource.ConnectType = DataSource.ConnectType;
            TargetSource.DataBase = DataSource.DataBase;
            TargetSource.PassWord = DataSource.PassWord;
            TargetSource.UserID = DataSource.UserID;
            TargetSource.Server = DataSource.Server;
        }

        private void DXWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            { btnOK_Click(null, null); }
            else if (e.Key == Key.Escape)
            { btnCanel_Click(null, null); }
        }

        private void cbTargetDB_SelectedIndexChanged(object sender, RoutedEventArgs e)
        {
            SetConnectionModel(TargetSource, this.cbTargetDB.Text);
        }

        private void cbSource_SelectedIndexChanged(object sender, RoutedEventArgs e)
        {
            SetConnectionModel(DataSource, this.cbSourceDB.Text);
        }
        #endregion

        /// <summary>
        /// 设置 下拉框选择的值
        /// </summary>
        /// <param name="connection"></param>
        private void SetConnectionModel(ConnectionModel connection,string database)
        {
            ConnectionModel serverConnection = null;
            if (connection.Server != string.Empty)
            {
                serverConnection = _connectList.FirstOrDefault(o => o.Server == connection.Server && o.DataBase == database);
            }
            if (serverConnection == null)
            {
                serverConnection = _connectList.FirstOrDefault(o => o.DataBase == database);
            }
            if (serverConnection != null)
            {
                connection.ConnectType = serverConnection.ConnectType;
                connection.DataBase = serverConnection.DataBase;
                connection.PassWord = serverConnection.PassWord;
                connection.Server = serverConnection.Server;
                connection.UserID = serverConnection.UserID;
            }
        }
    }
}
